package com.library.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.library.dto.pojo.PageBean;
import com.library.dto.vo.ReaderBorrow;
import com.library.entity.Reader;
import com.library.service.IReaderBorrowService;
import com.library.service.serviceImpl.PageServiceImpl;

@Controller
@RequestMapping("/reader")
public class ReaderBorrowController {
	
	public final int pageSize = 5;
	public int count;
	
	@Autowired
	private IReaderBorrowService borrowService;
	
	@RequestMapping("/borrow/{pageNum}")
	public String getBorrows(Model model, @PathVariable(value = "pageNum") int pageNum,HttpSession session) {
		Reader reader =(Reader)session.getAttribute("reader");
		Integer readerId = reader.getReaderId();
		EntityWrapper<ReaderBorrow> entityWrapper = new EntityWrapper<ReaderBorrow>();
		entityWrapper.in("libCardId",String.valueOf(readerId));
		count = borrowService.selectCount(entityWrapper);
		Page<ReaderBorrow> page = borrowService.selectPage(new Page<ReaderBorrow>(pageNum,pageSize),entityWrapper);
		List<ReaderBorrow> list = page.getRecords();
		PageBean<ReaderBorrow> pageBean = new PageServiceImpl().readerPage(list, pageNum, pageSize, count);
		model.addAttribute("bean", pageBean);
		model.addAttribute("select", false);
		return "reader/readerborrow";
	}
	
	@RequestMapping("/borrow/select")
	public String borrowSelect(ReaderBorrow borrow,Model model,HttpSession session) {
		Reader reader =(Reader)session.getAttribute("reader");
		Integer readerId = reader.getReaderId();
		EntityWrapper<ReaderBorrow> entityWrapper = new EntityWrapper<ReaderBorrow>();
		entityWrapper.in("libCardId",String.valueOf(readerId));
		if(borrow.getBorrowId()!=null && !borrow.getBorrowId().equals("")) {
			entityWrapper.eq("borrowId", String.valueOf(borrow.getBorrowId()));
		}
		if(borrow.getIsbn()!=null && !borrow.getIsbn().equals("")) {
			entityWrapper.eq("isbn",borrow.getIsbn());
		}
		count = borrowService.selectCount(entityWrapper);
		Page<ReaderBorrow> page = borrowService.selectPage(new Page<ReaderBorrow>(1,pageSize),entityWrapper);
		List<ReaderBorrow> list = page.getRecords();
		PageBean<ReaderBorrow> pageBean = new PageServiceImpl().readerPage(list, 1, pageSize, count);
		model.addAttribute("bean", pageBean);
		model.addAttribute("select", true);
		return "reader/readerborrow";
	}
}
